package com.iris.live.services.common;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by lusong on 2016/12/1.
 */
public class WeekOfMonthUtils {
    private static Logger logger = LoggerFactory.getLogger(WeekOfMonthUtils.class);

    public static List<Map> planWeek(String yearMonth, int weekNum) {
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyyMM");
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyyMMdd");
        Calendar calendar = Calendar.getInstance();
        List<Map> resultList = new ArrayList<>();
        try {
            int index = 1;
            List<Map> list = new ArrayList();
            calendar.setTime(sdf1.parse(yearMonth));
            int days = calendar.getActualMaximum(Calendar.DATE);
            //获取第一周天数及开始、结束日期
            calendar.setTime(sdf2.parse(yearMonth + "01"));
            int firstDayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
            Map map1 = new HashMap();
            map1.put("开始日期", yearMonth + "01");
            map1.put("周数", index++);
            if (firstDayOfWeek == 6) {
                map1.put("天数", 10);
                map1.put("结束日期", yearMonth + "10");
            } else if (firstDayOfWeek == 7) {
                map1.put("天数", 9);
                map1.put("结束日期", yearMonth + "09");
            } else if (firstDayOfWeek == 1) {
                map1.put("天数", 8);
                map1.put("结束日期", yearMonth + "08");
            } else {
                map1.put("天数", 9 - firstDayOfWeek);
                map1.put("结束日期", yearMonth + "0" + (9 - firstDayOfWeek));
            }
            list.add(map1);
            //获取最后一周天数及开始、结束日期
            calendar.setTime(sdf2.parse(yearMonth + days));
            int lastDayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
            Map mapLast = new HashMap();
            mapLast.put("结束日期", yearMonth + days);
            if (lastDayOfWeek == 1) {
                mapLast.put("天数", 7);
                mapLast.put("开始日期", yearMonth + (days - 6));
            } else if (lastDayOfWeek == 7) {
                mapLast.put("天数", 6);
                mapLast.put("开始日期", yearMonth + (days - 5));
            } else if (lastDayOfWeek == 6) {
                mapLast.put("天数", 5);
                mapLast.put("开始日期", yearMonth + (days - 4));
            } else if (lastDayOfWeek == 5) {
                mapLast.put("天数", 4);
                mapLast.put("开始日期", yearMonth + (days - 3));
            } else {
                mapLast.put("天数", 6 + lastDayOfWeek);
                mapLast.put("开始日期", yearMonth + (days - 5 - lastDayOfWeek));
            }
            //list.add(mapLast);
            //获取每周天数及开始、结束日期
            int num = (days - Integer.parseInt(map1.get("天数").toString()) - Integer.parseInt(mapLast.get("天数").toString())) / 7;
            for (int i = 0; i < num; i++) {
                Map mapN = new HashMap();
                mapN.put("周数", index++);
                mapN.put("天数", 7);
                if (i == 0 && Integer.parseInt(map1.get("天数").toString()) < 9) {
                    mapN.put("开始日期", yearMonth + "0" + (Integer.parseInt(map1.get("天数").toString()) + 7 * i + 1));
                } else {
                    mapN.put("开始日期", yearMonth + (Integer.parseInt(map1.get("天数").toString()) + 7 * i + 1));
                }
                mapN.put("结束日期", yearMonth + (Integer.parseInt(map1.get("天数").toString()) + 7 * i + 7));
                list.add(mapN);
            }
            mapLast.put("周数", index++);
            list.add(mapLast);

            if (weekNum > 0) {
                resultList.add(list.get(weekNum - 1));
            } else {
                resultList = list;
            }
        } catch (ParseException e) {
            logger.error("周计划异常。", e);
        }
        return resultList;
    }
}
